package com.yc.jpyy.vehicle.controller;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.common.utils.manydatasource.HandlerDataSource;
import com.yc.jpyy.base.controller.BaseController;
import com.yc.jpyy.utils.Tools;
import com.yc.jpyy.vehicle.dao.VehicleDao;
import com.yc.jpyy.vehicle.service.VehicleService;

@Controller
@RequestMapping(value = "/vehicle")
@Scope("prototype")
public class VehicleOperateLogController extends BaseController{
		private static Logger logger = LoggerFactory.getLogger(VehicleOperateLogController.class);

		@Autowired
		private VehicleService vehicleService;

		@Autowired
		private VehicleDao vehicleDao;
		
		/**
		 * 插入车辆操作日志
		 * 
		 * @param request
		 * @return
		 * @throws UnsupportedEncodingException
		 */
		@SuppressWarnings("finally")
		@RequestMapping(value = "/addVehicleOperateLog", method = { RequestMethod.GET,
				RequestMethod.POST }, produces = "application/json; charset=utf-8")
		public @ResponseBody String addVehicleOperateLog(HttpServletRequest request) throws UnsupportedEncodingException {

			Map<String,Object> resultMap = new HashMap<>();
			request.setCharacterEncoding("UTF-8");
			try {
				String vehicleOperateInfo = request.getParameter("info");
				Map<String,Object> paramsMap = Tools.JsonToMap(vehicleOperateInfo);
				String dbcode = paramsMap.get("dbcode").toString();
				HandlerDataSource.setDataSource(dbcode);
				resultMap = vehicleService.addVehicleOperateLog(paramsMap);
				resultMap.put("pubKey",Optional.ofNullable(request.getAttribute("pubKey")).orElse(""));
			} catch (Exception e) {
				logger.info("addVehicleOperateLog" + e.toString());
				resultMap.put("returnCode ", 0);
				resultMap.put("returnMsg ", "添加失败");
			} finally {
				HandlerDataSource.clear();
				return JSON.toJSONString(resultMap);
			}
		}
		
		/**
		 * 查询车辆操作日志记录
		 * 
		 * @param request
		 * @return
		 * @throws UnsupportedEncodingException
		 */
		@SuppressWarnings("finally")
		@RequestMapping(value = "/queryVehicleOperateLog", method = { RequestMethod.GET,
				RequestMethod.POST }, produces = "application/json; charset=utf-8")
		public @ResponseBody String queryVehicleOperateLog(HttpServletRequest request) throws UnsupportedEncodingException {

			Map<String,Object> resultMap = new HashMap<>();
			request.setCharacterEncoding("UTF-8");
			try {
				String queryParameter= request.getParameter("info");
				Map<String,Object> paramsMap = Tools.JsonToMap(queryParameter);
				String dbcode = paramsMap.get("dbcode").toString();
				HandlerDataSource.setDataSource(dbcode);
				resultMap = vehicleService.queryVehicleOperateLog(paramsMap);
				resultMap.put("pubKey",Optional.ofNullable(request.getAttribute("pubKey")).orElse(""));
			} catch (Exception e) {
				logger.info("queryVehicleOperateLog" + e.toString());
				resultMap.put("returnCode", 0);
				resultMap.put("returnMsg", "查询失败");
				//resultMap.put("exception", "queryVehicleOperateLog" + e.toString());
			} finally {
				HandlerDataSource.clear();
				return JSON.toJSONString(resultMap);
			}
		}
		
		/**
		 * 查询车辆操作日志详情
		 * 
		 * @param request
		 * @return
		 * @throws UnsupportedEncodingException
		 */
		@SuppressWarnings("finally")
		@RequestMapping(value = "/queryVehicleOperateById", method = { RequestMethod.GET,
				RequestMethod.POST }, produces = "application/json; charset=utf-8")
		public @ResponseBody String queryVehicleOperateById(HttpServletRequest request) throws UnsupportedEncodingException {

			Map<String,Object> resultMap = new HashMap<>();
			request.setCharacterEncoding("UTF-8");
			try {
				String queryParameter= request.getParameter("info");
				Map<String,Object> paramsMap = Tools.JsonToMap(queryParameter);
				String dbcode = paramsMap.get("dbcode").toString();
				HandlerDataSource.setDataSource(dbcode);
				resultMap = vehicleService.queryVehicleOperateById(Integer.parseInt(paramsMap.get("id").toString()));
				resultMap.put("pubKey",Optional.ofNullable(request.getAttribute("pubKey")).orElse(""));
			} catch (Exception e) {
				logger.info("queryVehicleOperateById" + e.toString());
				resultMap.put("returnCode", 0);
				resultMap.put("returnMsg", "查询失败");
				//resultMap.put("exception", "queryVehicleOperateById" + e.toString());
			} finally {
				HandlerDataSource.clear();
				return JSON.toJSONString(resultMap);
			}
		}


}
